package com.ctshk.rpc.air.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.air.dto.AirInitializationSettingsChannelDataDto;
import com.ctshk.rpc.air.dto.AirInitializationSettingsDataDto;
import com.ctshk.rpc.air.entity.AirInitializationSettings;
import com.ctshk.rpc.air.entity.AirInitializationSettingsChannel;
import com.ctshk.rpc.air.mapper.AirInitializationSettingsChannelMapper;
import com.ctshk.rpc.air.mapper.AirInitializationSettingsMapper;
import com.ctshk.rpc.air.req.AirInitializationSettingsChannelDataReq;
import com.ctshk.rpc.air.req.AirInitializationSettingsDataReq;
import com.ctshk.rpc.air.req.AirInitializationSettingsQueryReq;
import com.ctshk.rpc.air.service.IAirInitializationSettingsService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 机票api初始化设置表 服务实现类
 * </p>
 *
 * @author lihang
 * @since 2021-03-05
 */
@DubboService
public class AirInitializationSettingsServiceImpl extends ServiceImpl<AirInitializationSettingsMapper, AirInitializationSettings> implements IAirInitializationSettingsService {

    @Autowired
    private AirInitializationSettingsMapper airInitializationSettingsMapper;
    @Autowired
    private AirInitializationSettingsChannelMapper airInitializationSettingsChannelMapper;

    @Override
    public Result update(AirInitializationSettingsDataReq req, TokenUser tokenUser) {
        /**通过供应商Id查询 加幅是否存在 如果不存在 则新增 存在即修改*/
        QueryWrapper<AirInitializationSettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AirInitializationSettings::getSupplierId,req.getSupplierId());
        AirInitializationSettings airInitializationSettings =  airInitializationSettingsMapper.selectOne(queryWrapper);
        Long id = -1l;
        if(null==airInitializationSettings){
            airInitializationSettings = EntityUtil.copy(req,AirInitializationSettings.class);
            id = IdWorker.getId();
            airInitializationSettings.setId(id);
            airInitializationSettings.setIsDeleted(IsDeletedCode.NO.getCode());
            airInitializationSettings.setCreateId(tokenUser.getId());
            airInitializationSettings.setModifiedId(tokenUser.getId());
            airInitializationSettings.setGmtCreate(LocalDateTime.now());
            airInitializationSettings.setGmtModified(LocalDateTime.now());
            airInitializationSettingsMapper.insert(airInitializationSettings);
        }else{
            id = airInitializationSettings.getId();
            airInitializationSettings = EntityUtil.copy(req,AirInitializationSettings.class);
            airInitializationSettings.setId(id);
            airInitializationSettingsMapper.updateById(airInitializationSettings);
            /**删除存在的渠道信息*/
            QueryWrapper<AirInitializationSettingsChannel> settingsChannelQueryWrapper = new QueryWrapper<>();
            settingsChannelQueryWrapper.lambda().eq(AirInitializationSettingsChannel::getSettingId,id);
            airInitializationSettingsChannelMapper.delete(settingsChannelQueryWrapper);
        }
        List<AirInitializationSettingsChannelDataReq>  initializationSettingsChannelReqs = req.getInitializationSettingsChannelReqList();
        for (AirInitializationSettingsChannelDataReq airInitiChannelReq : initializationSettingsChannelReqs) {
            AirInitializationSettingsChannel airInitiChannel = EntityUtil.copy(airInitiChannelReq,AirInitializationSettingsChannel.class);
            airInitiChannel.setId(IdWorker.getId());
            airInitiChannel.setSettingId(id);
            airInitiChannel.setGmtModified(LocalDateTime.now());
            airInitiChannel.setGmtCreate(LocalDateTime.now());
            airInitializationSettingsChannelMapper.insert(airInitiChannel);
        }
        return Result.success(id);
    }

    @Override
    public Result<List<AirInitializationSettingsDataDto>> query(AirInitializationSettingsQueryReq req) {
        QueryWrapper<AirInitializationSettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(ObjectUtils.isNotEmpty(req.getSupplierIds()),AirInitializationSettings::getSupplierId,req.getSupplierIds());
        List<AirInitializationSettings> airInitializationSettingsList = airInitializationSettingsMapper.selectList(queryWrapper);
        /**查询渠道信息*/
        Set<Long> settingIdSet = Sets.newHashSet();
        for (AirInitializationSettings airInitializationSettings : airInitializationSettingsList) {
            settingIdSet.add(airInitializationSettings.getId());
        }
        QueryWrapper<AirInitializationSettingsChannel> channelQueryWrapper = new QueryWrapper<>();
        channelQueryWrapper.lambda().in(ObjectUtils.isNotEmpty(settingIdSet),AirInitializationSettingsChannel::getSettingId,settingIdSet);
        List<AirInitializationSettingsChannel> initializationSettingsChannelList = airInitializationSettingsChannelMapper.selectList(channelQueryWrapper);
        /**按照Map分组后组装*/
        Map<Long,List<AirInitializationSettingsChannel>> assmbleMap = Maps.newHashMap();
        for (AirInitializationSettingsChannel airInitializationSettingsChannel : initializationSettingsChannelList) {
            List<AirInitializationSettingsChannel> mapValue = assmbleMap.get(airInitializationSettingsChannel.getSettingId());
            if(null==mapValue){
                mapValue = Lists.newArrayList();
            }
            mapValue.add(airInitializationSettingsChannel);
            assmbleMap.put(airInitializationSettingsChannel.getSettingId(),mapValue);
        }
        List<AirInitializationSettingsDataDto> resultList = Lists.newArrayList();
        for (AirInitializationSettings airInitializationSettings : airInitializationSettingsList) {
            AirInitializationSettingsDataDto airInitializationSettingsDataDto =  EntityUtil.copy(airInitializationSettings,AirInitializationSettingsDataDto.class);
            List<AirInitializationSettingsChannel> mapValue = assmbleMap.get(airInitializationSettings.getId());
            if(CollectionUtils.isNotEmpty(mapValue)){
                List<AirInitializationSettingsChannelDataDto> channelDataDtos = EntityUtil.copyList(mapValue,AirInitializationSettingsChannelDataDto.class);
                airInitializationSettingsDataDto.setInitializationSettingsChannelDataDtoList(channelDataDtos);
            }
            resultList.add(airInitializationSettingsDataDto);
        }
        return Result.success(resultList);
    }


}
